# -*- coding: utf-8 -*-
# !/usr/bin/env python3

"""
@Author   : Leonis.Li
@Email    : leonis.li.ext@zeiss.com
@Cellphone:
@Version  : 1.0
@License  : Apache Licence
@Script   : ocr_service.py
@CreateAt : 2023/10/11 21:35
@UpdateAt : 2023/10/11 21:35
"""

"""
# 参考文档: https://pythonology.eu/what-is-the-best-python-pdf-library/ 
# 包流行度: PyPDF == pdfplumber >> PyMuPDF


01.PyPDF     : used to read & write PDF files, extract text, merge & split, encrypt & decrypt PDFs (Popular  and has been around for a long time)
02.PyMuPDF   : The best library; support file( extensions: “.pdf”, “.xps”, “.oxps”, “.cbz”, “.fb2”, “.mobi” or “.epub”) & 10 popular image formats;
               网址: https://pymupdf.readthedocs.io/en/latest/tutorial.html 
03.pdfplumber: Allows for extract "tables & text & images & shapes" from PDFs; 
04.pdfminer  : Popular text extraction tool for PDF documents; 网址: https://pypi.org/project/pdfminer/;
05.tabula-py : Simple Python wrapper of tabula-java, read tables in PDFS, and convert "read data" to csv/tsv, json, pandas-dataframe; 网址: https://pypi.org/project/tabula-py/ 
"""


# pymupdf
import fitz
# pdfplumber
import pdfplumber
# pypdf
from pypdf import PdfReader


def how_to_use_pypdf(filepath="/home/user/data/report.pdf"):
    """ 介绍如何使用 pypdf 包"""

    reader = PdfReader(filepath)

    # Print the number of pages in the PDF
    print(f"There are {len(reader.pages)} Pages")

    # Get the first page (index 0) 
    page = reader.pages[0]
    # Use extract_text() to get the text of the page
    print(page.extract_text())

    # Go through every page and get the text
    for page_no, page_obj in enumerate(reader.pages, start=1):
        # 通过索引获取 page object
        page = reader.pages[page_no]
        
        print(page_obj.extract_text())
    
    # 写入文件
    for img in page.images:
        with open(img.name, "wb") as fp:
            fp.write(img.data)



def how_to_use_pdfplumber(filepath="/home/user/data/report.pdf"):
    """ 介绍如何使用 pdfplumber 包"""

    with pdfplumber.open(filepath) as pdf:
        # iterate over each page
        for page in pdf.pages:
            # extract text & table
            text = page.extract_text()
            table = page.extract_tables()
            print(text)
            print(table)


def how_to_use_pymupdf(filepath="/home/user/data/report.pdf"):
    """ 介绍如何使用 pymupdf 包"""

    # Create a document object
    doc = fitz.open('file.pdf')  # or fitz.Document(filename)

    # Extract the number of pages (int)
    print(doc.page_count)

    # the metadata (dict) e.g., the author,...
    print(doc.metadata)

    # Get the page by their index
    page = doc.load_page(0)
    # or page = doc[0]

    # read a Page
    text = page.get_text()
    print(text)

    # Render and save the page as an image
    pix = page.get_pixmap() 
    pix.save(f"page-{page.number}.png")

    # get all links on a page
    links = page.get_links()
    print(links)

    # Render and save all the pages as images
    for i in range(doc.page_count):
        page = doc.load_page(i)
        pix = page.get_pixmap()
        pix.save("page-%i.png" % page.number)

    # get the links on all pages
    for i in range(doc.page_count):
        page = doc.load_page(i)
        link = page.get_links()
        print(link)
